//
//  ZJPullViewController.m
//  zjzb
//
//  Created by Mac on 2017/3/24.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "ZJPullViewController.h"
#import "XLHotTableViewCell.h"
#import "XLHotData.h"
#import "XLLiveTool.h"
#import "ZJPlayViewController.h"
#import "SXTLocationManager.h"
static NSString *reuseIdentifier = @"cell";
@interface ZJPullViewController ()
/** 直播 */
@property(nonatomic, strong) NSMutableArray *allModels;

/** 当前页 */
@property(nonatomic, assign) NSUInteger currentPage;

/** 返回数据 */
@property (nonatomic, strong) XLHotData *hotData;
@end

@implementation ZJPullViewController
- (void)loadNewData
{
    self.currentPage = 1;
    __weak typeof(self) weakSelf = self;
    [XLLiveTool GetWithHotURL:self.currentPage success:^(XLNewResult *result) {
        
        NSLog(@"%@",result);
        
        if (weakSelf.currentPage == 1){
            [weakSelf.allModels removeAllObjects];
        }
        
        XLHotData *hotData = [XLHotData mj_objectWithKeyValues:result.data];
        weakSelf.hotData = hotData;
        
      
        
        if (hotData.list){
            
            [weakSelf.allModels addObjectsFromArray:hotData.list];
            
            
            //重新加载
            [weakSelf.tableView reloadData];
            
        }else{
            
          
            
            weakSelf.currentPage--;
        }
        
    } failure:^(NSError *error) {
        
       
        weakSelf.currentPage--;
        
        
    }];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadNewData];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;

    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([XLHotTableViewCell class]) bundle:nil] forCellReuseIdentifier:
     reuseIdentifier];
    
    [[SXTLocationManager sharedManager]getGps:^(NSString *lat, NSString *lon) {
        NSLog(@"%@,%@",lat,lon);
    }];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return self.allModels.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    XLHotTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    
    XLHotModel *hotModel = self.allModels[indexPath.item];

    cell.hotModel = hotModel;
    cell.allModels = self.hotData.list;
    cell.parentVC = self;
    
    
    return cell;
    
}
- (NSMutableArray *)allModels
{
    if (_allModels == nil) {
        _allModels = [NSMutableArray array];
        
    }
    return _allModels;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    XLHotTableViewCell *cell = [XLHotTableViewCell tableViewCell];
    
    
    cell.hotModel = self.allModels[indexPath.item];
    
    
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJPlayViewController *player = [[ZJPlayViewController alloc]init];
    XLHotModel *hotModel = self.allModels[indexPath.item];
    player.hotModel = hotModel;
    [self.navigationController pushViewController:player animated:YES];
    
    
    
    
}

@end
